#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

void* Calloc(size_t nmemb, size_t size) {
    void *ret = calloc(nmemb, size);
    if (!ret) {
        perror("calloc");
        exit(1);
    }
    return ret;
}

void allocate_align(void **p, size_t size) {
    if (posix_memalign(p, size, size)) {
        perror("posix_memalign failed");
        exit(1);
    }
}

void Pwrite(int fd, const void *buf, size_t size, off_t offset) {
    if (pwrite(fd, buf, size, offset) != size) {
        perror("pwrite failed");
        exit(1);
    }
}

void* Malloc(size_t size) {
    void *ret = malloc(size);
    if (!ret) {
        perror("malloc");
        exit(1);
    }
    return ret;
}

int OpenMode(const char *pathname, int flags, mode_t mode) {
    int fd = open(pathname, flags, mode);
    if (unlikely(fd < 0)) {
        perror("OpenMode");
        exit(-1);
    }
    return fd;
}

int Open(const char *pathname, int flags) {
    int fd = open(pathname, flags);
    if (unlikely(fd < 0)) {
        perror("Open");
        exit(-1);
    }
    return fd;
}
